import pika
import pymysql
import json

status_data = {
    1: 'cpu_used', 2: 'memory_total', 3: 'memory_used', 4: 'memory_free', 5: 'one_load', 6: 'five_load',
    7: 'fivteen_load', 8: 'run_time', 9: 'disk_total', 10: 'disk_used',
    11: 'disk_free', 12: 'in_master_identity', 13: 'in_slave_identity', 14: 'out_master_identity',
    15: 'out_slave_identity', 16: 'db_conn', 18: 'mails', 19: 'web_mem', 20: 'websocket', 21: 'exchange_mem',
    22: 'binlog_mem', 23: 'voice_arm', 24: 'led', 25: 'io_module', 26: 'printer', 27: 'ticker_printer',
    28: 'in_gate_num', 29: 'in_gate_status', 30: 'out_gate_num', 31: 'out_gate_status'}


def makeData(data):
    result = {}
    for x in data:
        if x['code'] in status_data:
            result[status_data[x['code']]] = x['value']

    for x, y in status_data.items():
        if y not in result:
            result[y] = 0
    return result


credentials = pika.PlainCredentials('guest', 'guest')
connection = pika.BlockingConnection(pika.ConnectionParameters('180.97.80.42', 5672, '/', credentials))
channel = connection.channel()
channel.queue_declare(queue='i2p_test')
channel.queue_bind(exchange='arm_msg_broadcast_ex_test', queue='i2p_test')

connect = pymysql.connect(host='180.97.80.42', port=10086, user='root', passwd='iraintest', db='i2p')


def callback(ch, method, properties, body):
    body = bytes.decode(body)
    if body:
        str = json.loads(body)
        if str['cmd'] == 2:
            data = json.loads(str['data']['list'])
            result = makeData(data)
            print(result)
            with connect.cursor() as cursor:
                sql = "INSERT INTO t_arm_status_history (park_code,arm_code,cpu_used,memory_total,memory_used,memory_free,one_load,five_load,fivteen_load,run_time,disk_total,disk_used,disk_free,in_master_identity,in_slave_identity,out_master_identity,out_slave_identity,db_conn,mails,web_mem,websocket,exchange_mem,binlog_mem,voice_arm,led,io_module,printer,ticker_printer,in_gate_num,in_gate_status,out_gate_num,out_gate_status,createon) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
                cursor.execute(sql, (
                    str['data']['park_code'], int(str['data']['arm_code']), result['cpu_used'], result['memory_total'],
                    result['memory_used'], result['memory_free'], result['one_load'], result['five_load'],
                    result['fivteen_load'], result['run_time'], result['disk_total'], result['disk_used'],
                    result['disk_free'], result['in_master_identity'], result['in_slave_identity'],
                    result['out_master_identity'], result['out_slave_identity'], result['db_conn'], result['mails'],
                    result['web_mem'], result['websocket'], result['exchange_mem'], result['binlog_mem'],
                    result['voice_arm'], result['led'], result['io_module'], result['printer'],
                    result['ticker_printer'],
                    result['in_gate_num'], result['in_gate_status'], result['out_gate_num'], result['out_gate_status'],
                    str['data']['update_time']))
            connect.commit()


channel.basic_consume(callback, queue='i2p_test', no_ack=False)
channel.start_consuming()

connect.close()
